﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Collections;
public partial class User_Employer_EditContract : System.Web.UI.Page
{
    
    private Guid _Contractid;
    public Guid Contractid
    {
        get { return _Contractid; }
        set
        {
            _Contractid = value;
        }
    }

    private string _Date;
    public string Date
    {
        get { return _Date; }
        set
        {
            _Date = value;
        }
    }

    private string _Role;
    public string Role
    {
        get { return _Role; }
        set
        {
            _Role = value;
        }
    }

    private string _VillageName;
    public string VillageName
    {
        get { return _VillageName; }
        set
        {
          _VillageName = value;
        }
    }
    private string _ContractNumber;
    public string ContractNumber
    {
        get { return _ContractNumber; }
        set
        {
            _ContractNumber = value;
        }
    }
    private string _UserId;
    public string UserId
    {
        get { return _UserId; }
        set
        {
            _UserId = value;
        }
    }
    private string _FUserId;
    public string FUserId
    {
        get { return _FUserId; }
        set
        {
            _FUserId = value;
        }
    }
    private string _Family;
    public string Family
    {
        get { return _Family; }
        set
        {
            _Family = value;
        }
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
     

        if (!Page.IsPostBack)
        {
            
        }
       


    }
    protected void RadioButtonListSelector_SelectedIndexChanged(object sender, EventArgs e)
    {
        //RadioButtonList r=(RadioButtonList) LoginView2.FindControl("RadioButtonListSelector");
        //MultiView m = (MultiView)LoginView2.FindControl("MultiView1");
        //if (r.SelectedItem.Value == "Village")
        //{
        //    m.ActiveViewIndex = 0;
        //}
        //else
        //{
        //    m.ActiveViewIndex = 1;
        //}
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            if (RadioButtonListRole.SelectedItem.Value == "Advisor")
            {
                if (RadioButtonListAdvisors.SelectedValue == "")
                {
                    SubSonic.Query q1 = DataAccess.TblUserDetail.CreateQuery();
                    q1.QueryType = SubSonic.QueryType.Select;
                    q1.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q1.AND(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
                    q1.OR(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");

                    DataSet dt1 = q1.ExecuteDataSet();
                    if (dt1.Tables[0].Rows.Count > 0)
                    {
                        GridViewUsers.DataSource = dt1;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                        cleargrid();
                    }
                }
            }

            if (RadioButtonListRole.SelectedItem.Value == "Advisor")
            {
                if (RadioButtonListAdvisors.SelectedValue != "")
                {
                    if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhoghooghi")
                    {
                        SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                        q.QueryType = SubSonic.QueryType.Select;
                        q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                        q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
                        DataSet dt = q.ExecuteDataSet();
                        if (dt.Tables[0].Rows.Count > 0)
                        {
                            GridViewUsers.DataSource = dt;
                            GridViewUsers.DataBind();
                        }
                        else
                        {
                            Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                            cleargrid();
                        }
                    }
                    if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhaghighi")
                    {
                        SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                        q.QueryType = SubSonic.QueryType.Select;
                        q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                        q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");
                        DataSet dt = q.ExecuteDataSet();
                        if (dt.Tables[0].Rows.Count > 0)
                        {
                            GridViewUsers.DataSource = dt;
                            GridViewUsers.DataBind();
                        }
                        else
                        {
                            Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                            cleargrid();
                        }
                    }
                }
            }
            if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
            {
                if (RadioButtonListSurveyor.SelectedItem.Value == "Advisorhoghooghi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                        cleargrid();
                    }
                }
                if (RadioButtonListSurveyor.SelectedItem.Value == "Advisorhaghighi")
                {
                    SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                    q.QueryType = SubSonic.QueryType.Select;
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                    q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");
                    DataSet dt = q.ExecuteDataSet();
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        GridViewUsers.DataSource = dt;
                        GridViewUsers.DataBind();
                    }
                    else
                    {
                        Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                        cleargrid();
                    }
                }
            }
        }
        catch
        { }
    }
    protected void RadioButtonListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            RadioButtonListSurveyor.Visible = false;
            RadioButtonListAdvisors.Visible = true;
            Role = "Advisor";
        }
        else
        {
            RadioButtonListAdvisors.Visible = false;
            RadioButtonListSurveyor.Visible = true;
            Role = "Surveyor";
        }
        
    }
   

    public void cleargrid()
    {
        GridViewUsers.DataSource = null;
        GridViewUsers.DataBind();
    }
    protected void GridViewUsers_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridViewContracts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Business.Calender cal = new Business.Calender();
                string pdate = cal.MiladiToPersian(Convert.ToDateTime(e.Row.Cells[1].Text));
              string[] s= pdate.Split(new char[] {'/'});
              e.Row.Cells[1].Text = s[2].ToString() + "/" + s[1].ToString() + "/" + s[0].ToString();
                IDataReader t = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, e.Row.Cells[9].Text);
                if (t.Read())
                {
                    e.Row.Cells[2].Text = t["Name"].ToString() + " / " + t["Family"].ToString();
                    t.Close();
                }

                if (e.Row.Cells[3].Text == bool.TrueString)
                    e.Row.Cells[3].Text = "قرارداد مشاوره";
                if (e.Row.Cells[3].Text == bool.FalseString)
                    e.Row.Cells[3].Text = "قرارداد نقشه برداری";
                DataAccess.TblVillage vil = DataAccess.TblVillage.FetchByID(e.Row.Cells[11].Text);
                e.Row.Cells[5].Text = vil.Name;
                DataAccess.TblDehestan deh = DataAccess.TblDehestan.FetchByID(vil.DehstanId);
                e.Row.Cells[6].Text = deh.Name;
                DataAccess.TblBakhsh bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
                e.Row.Cells[7].Text = bakhsh.Name;
                DataAccess.TblCity city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
                e.Row.Cells[8].Text = city.Name;
                e.Row.Cells[9].Visible = false;
                e.Row.Cells[11].Visible = false;
            }
        }
        catch
        {
        }
    }

   
    protected void RadioButtonListAdvisors_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridViewContracts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Select":
                Guid idc = new Guid(GridViewContracts.DataKeys[id].Value.ToString());
                Contractid = idc;
                     Date=GridViewContracts.Rows[id].Cells[1].Text;
                 VillageName= GridViewContracts.Rows[id].Cells[5].Text;
                ContractNumber = GridViewContracts.Rows[id].Cells[0].Text;
                TxtContractNumber.Text = ContractNumber;
                Session["ContractNumber"] = ContractNumber;
           
                DataAccess.TblContractCollection con = new DataAccess.TblContractCollection();
                con.LoadAndCloseReader(DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.ContractNumber,ContractNumber));
               
           
                JQDatePicker1.Text = Date;
                 TxtYear.Text = con[0].Year.ToString();
                 Session["UserId"] = con[0].UserId;
                IDataReader dr = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.Role, "FourthFactor");
                DropDownListFourthFactor.ClearSelection();
                DropDownListFourthFactor.Items.Clear();
                while (dr.Read())
            {
                ListItem l = new ListItem();
                l.Text = dr["Name"].ToString() + " " + dr["Family"].ToString();
                l.Value = dr["UserId"].ToString();
                DropDownListFourthFactor.Items.Add(l);
                DropDownListFourthFactor.DataBind();
            }
            dr.Close();

          Session["NameFamily"]= Business.CommonCalculations.FindDoerOfProject(Contractid);
          LblNameFamily.Text = Session["NameFamily"].ToString();
                 for (int i = 0; i <= DropDownListFourthFactor.Items.Count-1; i++)
                     if (DropDownListFourthFactor.Items[i].Value == con[0].FourthFactorUserId.ToString())
                         DropDownListFourthFactor.Items[i].Selected = true;
                     //DropDownListFourthFactor.Items.FindByValue(con[0].FourthFactorUserId.ToString()).Selected=true;
                     if (Session["UserIdUpload"] != null)
                     {
                         MembershipUser u = Membership.GetUser(Session["UserIdUpload"]);
                         if (Roles.IsUserInRole(u.UserName, "Advisor"))
                         {

                             Role = "Advisor";
                         }
                         if (Roles.IsUserInRole(u.UserName, "Surveyor"))
                         {

                             Role = "Surveyor";
                         }
                     }
                  List<Guid> villages=new List<Guid>();
                  Session["villages"] = "";
                  ListVillages.Items.Clear();
                foreach (DataAccess.TblContract c in con)
                {
                villages.Add(c.VillageId);
                Session["villages"] = villages;
                ListItem l = new ListItem();
                DataAccess.TblVillage vil = DataAccess.TblVillage.FetchByID(c.VillageId);
                l.Text = vil.Name;
                    l.Value = c.VillageId.ToString();
                ListVillages.Items.Add(l);
                ListVillages.DataBind();
                }

                MultiViewAdmin.ActiveViewIndex = 3;
                //RadioButtonListRole.Visible = false;;
                //return Contractid;
                
                break;
        }
    }



    

    protected void ButtonCancelAdmin_Click(object sender, EventArgs e)
    {
        clearadmin();
        MultiViewAdmin.ActiveViewIndex = 0;
    }
    public void clearadmin()
    {
        TxtFamily.Text = "";
      
        GridViewContracts.DataSource = null;
        GridViewContracts.DataBind();
        GridViewUsers.DataSource = null;
        GridViewUsers.DataBind();
       
        MultiViewAdmin.ActiveViewIndex = 0;
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        clearadmin();
        MultiViewAdmin.ActiveViewIndex = -1;
    }

    protected void GridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Select":
                Guid userid = new Guid(GridViewUsers.DataKeys[id].Value.ToString());
                UserId = userid.ToString();
                Family = GridViewUsers.Rows[id].Cells[0].Text + " " + GridViewUsers.Rows[id].Cells[1].Text;

                Session["UserId"] = userid;
           
                 DataAccess.TblContractCollection con=new DataAccess.TblContractCollection();
                 con.LoadAndCloseReader(DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.UserId, SubSonic.Comparison.Equals, userid, SubSonic.OrderBy.Asc("ContractNumber")));
                 //con.
                 //IDataReader dr=;
                // List<DataAccess.TblContract> conlist = new List<DataAccess.TblContract>();
                // //var qry = from cus in con group cus by new { cus.ContractNumber };
                // //var qry = con.SelectMany(p => p.ContractNumber).Distinct();
                //var x = from t in con  select new { t.ContractDate, t.Id, t.ContractNumber.Distinct() };
                // foreach (DataAccess.TblContract qr in qry)
                //     conlist.Add(qr);
                 
                 GridViewContracts.DataSource = con;
                 GridViewContracts.DataBind();
                 MultiViewAdmin.ActiveViewIndex = 2;
                break;
        }
    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedItem.Value == "Family")
        {
            MultiViewAdmin.ActiveViewIndex = 1;
        } 
        if (RadioButtonList1.SelectedItem.Value == "Village")
        {
            MultiViewAdmin.ActiveViewIndex = 0;
        }
    }
  protected void  DropDownVillage1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        GridViewContracts.DataSource = DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.VillageId, SubSonic.Comparison.Equals, DropDownVillage1.SelectedValue, SubSonic.OrderBy.Asc("ContractNumber"));
        GridViewContracts.DataBind();
        MultiViewAdmin.ActiveViewIndex = 2;
    }
    catch
    { }
    //Page.DataBind();
}
  protected void DropDownVillage_SelectedIndexChanged(object sender, EventArgs e)
  {
      try
      {
          GridViewVillageDetail.DataSource = DataAccess.TblVillage.FetchByParameter(DataAccess.TblVillage.Columns.Id, SubSonic.Comparison.Equals, DropDownvillage.SelectedValue);
          GridViewVillageDetail.DataBind();
    
      }
      catch
      { }
      //Page.DataBind();
  }
  protected void DropDownVillage_TextChanged(object sender, EventArgs e)
  {

  }
  protected void GridViewContracts_SelectedIndexChanged(object sender, EventArgs e)
  {

  }

  protected void ListVillages_SelectedIndexChanged(object sender, EventArgs e)
  {

  }

  protected void LinkButtonInsert_Click(object sender, EventArgs e)
  {
      try
      {
          if (DropDownvillage.SelectedValue != "")
          {
              //DropDownList dp = (DropDownList)SelectCity1.FindControl("DropDownListVillage");
              List<Guid> vil = Session["Villages"] as List<Guid>;
              vil.Add(new Guid(DropDownvillage.SelectedValue));
              Session["Village"] = vil;
              filllist();



              DataAccess.TblContract contract = new DataAccess.TblContract();
              contract.VillageId = new Guid(DropDownvillage.SelectedValue);
              //string s = Request.Form["txtDate"].ToString();
              string s = JQDatePicker1.Text;
              string[] split = s.Split(new Char[] { '/' });
              Business.Calender calender = new Business.Calender();
              DateTime Date = calender.PersianToMiladi(split[2].ToString(), split[1].ToString(), split[0].ToString());
              //MembershipUser mu= Membership.GetUser(DropDownAdvisor.SelectedItem.Text);
              contract.UserId = (Guid)Session["UserId"];
              contract.AdvisorName = LblNameFamily.Text;
              contract.ContractDate = Date;
              contract.Year = Convert.ToInt32(TxtYear.Text);
              contract.FourthFactorUserId = new Guid(DropDownListFourthFactor.SelectedItem.Value);
              //contract.AdvisorName = DropDownAdvisor.SelectedItem.Text;
              contract.ContractNumber = TxtContractNumber.Text;
              if (RadioButtonListRole.SelectedItem.Value == "Advisor")
              {
                  contract.ContractType = true;
                  contract.Save();
                  object pk = contract.GetPrimaryKeyValue();
                  Tasksinsert(pk);
              }
              if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
              {
                  contract.ContractType = false;
                  contract.Save();
                  object pk = contract.GetPrimaryKeyValue();
                  Tasksinsert(pk);
              }
              Lblstatus.CssClass = "success";
              Lblstatus.Text = "ثبت روستای جدید با موفقیت انجام شد";
          }
          else
          {
              Lblstatus.Text = " روستا بایستی انتخاب شود";
              Lblstatus.CssClass = "error";
          }
      }
      catch
      {
          Lblstatus.Text = "خطا در لیست روستاها";
          Lblstatus.CssClass = "error";
      }

  }

  public void filllist()
  {

      ListVillages.Items.Clear();
           List<Guid> villages=Session["villages"] as List<Guid>;
           
      foreach (Guid c in villages )
      {
 
          ListItem l = new ListItem();
          DataAccess.TblVillage vil = DataAccess.TblVillage.FetchByID(c);
          l.Text = vil.Name;
          l.Value = c.ToString();
          ListVillages.Items.Add(l);
          ListVillages.DataBind();
      }
  }

  protected void LinkButtonDelete_Click(object sender, EventArgs e)
  {
      if (ListVillages.Items.Count == 0)
      {
          Lblstatus.Text = ".لیست خالی میباشد";
          Lblstatus.CssClass = "error";
      }
      if (ListVillages.Items.Count > 0)
      {
         
          if (ListVillages.SelectedIndex >= 0)
          {
              SubSonic.Query qad = new SubSonic.Query(DataAccess.TblContract.Schema);
              qad.QueryType = SubSonic.QueryType.Delete;
              qad.WHERE(DataAccess.TblContract.Columns.ContractNumber, Session["ContractNumber"]);
              qad.WHERE(DataAccess.TblContract.Columns.UserId, Session["UserId"]);
              qad.WHERE(DataAccess.TblContract.Columns.VillageId, ListVillages.SelectedValue);
              qad.BuildDeleteCommand();
              qad.Execute();

              List<Guid> dt = Session["Villages"] as List<Guid>;
              dt.RemoveAt(ListVillages.SelectedIndex);
              Session["Villages"] = dt;
              filllist();
              Lblstatus.Text = ".روستای مورد نظر حذف گردید ";
              Lblstatus.CssClass = "success";
          }
          else
          {
              Lblstatus.Text = ".لطفا ابتدا روستا را انتخاب بفرمایید ";
              Lblstatus.CssClass = "error";
          }
      }


  }
  //public void clear()
  //{
  //    DataTable dt = (DataTable)Session["Village"];
  //    dt.Clear();
  //    Session["Village"] = dt;
  //    filllist();
  //    TxtContractNumber.Text = "";
  //    JQDatePicker1.Text = "";
  //    DropDownCity.ClearSelection();
  //    GridViewVillageDetail.DataSource = "";
  //    GridViewVillageDetail.DataBind();
  //    //Lbladdress.Text = "";
  //    //LblMobile.Text = "";
  //    //LblName.Text = "";
  //    //LblTel.Text = "";
  //}
  //protected void Button1_Click1(object sender, EventArgs e)
  //{
  //    try
  //    {
  //        DataTable dt = (DataTable)Session["Village"];
  //        if (dt.Rows.Count > 0)
  //        {
  //            for (int i = 0; i <= dt.Rows.Count - 1; i++)
  //            {
  //                DataAccess.TblContract contract = new DataAccess.TblContract();
  //                contract.VillageId = new Guid(dt.Rows[i]["ID"].ToString());
  //                //string s = Request.Form["txtDate"].ToString();
  //                string s = JQDatePicker1.Text;
  //                string[] split = s.Split(new Char[] { '/' });
  //                Business.Calender calender = new Business.Calender();
  //                DateTime Date = calender.PersianToMiladi(split[2].ToString(), split[1].ToString(), split[0].ToString());
  //                //MembershipUser mu= Membership.GetUser(DropDownAdvisor.SelectedItem.Text);
  //                contract.UserId = (Guid)Session["UserId"];
  //                contract.AdvisorName = LblNameFamily.Text;
  //                contract.ContractDate = Date;
  //                contract.Year = Convert.ToInt32(TxtYear.Text);
  //                contract.FourthFactorUserId = new Guid(DropDownListFourthFactor.SelectedItem.Value);
  //                //contract.AdvisorName = DropDownAdvisor.SelectedItem.Text;
  //                contract.ContractNumber = TxtContractNumber.Text;
  //                if (RadioButtonListRole.SelectedItem.Value == "Advisor")
  //                {
  //                    contract.ContractType = true;
  //                    contract.Save();
  //                    object pk = contract.GetPrimaryKeyValue();
  //                    Tasksinsert(pk);
  //                }
  //                if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
  //                {
  //                    contract.ContractType = false;
  //                    contract.Save();
  //                    object pk = contract.GetPrimaryKeyValue();
  //                    Tasksinsert(pk);
  //                }
  //            }


  //            Lblstatus.CssClass = "success";
  //            Lblstatus.Text = " .قرارداد با موفقیت ثبت گردید ";

  //            clear();

  //        }
  //        else
  //        {
  //            Lblstatus.Text = " .روستاهای مورد قرارداد را انتخاب کنید ";
  //            Lblstatus.CssClass = "error";
  //        }

  //    }
  //    catch
  //    {
  //        Lblstatus.Text = " .ثبت قرارداد باخطا مواجه شد ";
  //        Lblstatus.CssClass = "error";

  //    }
  //}
  public void Tasksinsert(object contractid)
  {
      if (RadioButtonListRole.SelectedItem.Value == "Advisor")
      {
          for (int j = 1; j <= 18; j++)
          {
              DataAccess.TblTask t = new DataAccess.TblTask();

              IDataReader dr = DataAccess.TblStepsAdvisor.FetchByParameter(DataAccess.TblStepsAdvisor.Columns.Priority, j);
              dr.Read();
              Guid drToString = new Guid(dr["ID"].ToString());
              t.AdvisorStepId = drToString;
              dr.Close();
              t.ContractId = (Guid)contractid;
              t.IsDonei = false;
              t.UserName = Membership.GetUser().UserName;

              t.Save();
          }
      }
      if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
      {
          for (int j = 1; j <= 8; j++)
          {
              DataAccess.TblTask t = new DataAccess.TblTask();

              IDataReader dr = DataAccess.TblStepsSurveyor.FetchByParameter(DataAccess.TblStepsSurveyor.Columns.Priority, j);
              dr.Read();
              Guid drToString = new Guid(dr["ID"].ToString());
              t.SurveyorStepId = drToString;
              dr.Close();
              t.ContractId = (Guid)contractid;
              t.IsDonei = false;
              t.UserName = Membership.GetUser().UserName;

              t.Save();
          }
      }

  }
  //protected void Button2_Click(object sender, EventArgs e)
  //{
  //    clear();
  //    cleargrid();
  //    MultiViewAdmin.ActiveViewIndex = 0;
  //}
  protected void ButtonSaveInfo_Click(object sender, EventArgs e)
  {
      try
      {
      SubSonic.Query q = DataAccess.TblContract.CreateQuery();
      q.QueryType = SubSonic.QueryType.Update;
      q.AddUpdateSetting(DataAccess.TblContract.Columns.ContractNumber,TxtContractNumber.Text);
      q.AddUpdateSetting(DataAccess.TblContract.Columns.Year, TxtYear.Text);
      if (Session["UserIdNew"] != null)
      q.AddUpdateSetting(DataAccess.TblContract.Columns.UserId, Session["UserIdNew"]);

      q.AddUpdateSetting(DataAccess.TblContract.Columns.FourthFactorUserId, DropDownListFourthFactor.SelectedItem.Value);
      string s = JQDatePicker1.Text;
      string[] split = s.Split(new Char[] { '/' });
      Business.Calender calender = new Business.Calender();
      DateTime Date = calender.PersianToMiladi(split[2].ToString(), split[1].ToString(), split[0].ToString());

      q.AddUpdateSetting(DataAccess.TblContract.Columns.ContractDate, Date);
      q.AddWhere("ContractNumber", SubSonic.Comparison.Equals, Session["ContractNumber"]);
      q.AddWhere("UserId", SubSonic.Comparison.Equals, Session["UserId"]);
      q.Execute();
      Lblstatus1.CssClass = "success";
      Lblstatus1.Text=" ویرایش اطلاعات اولیه قرارداد با موفقیت انجام شد.";
      Session["UserId"] = Session["UserIdNew"];
      }
      catch
      {
          Lblstatus1.CssClass = "error";
          Lblstatus1.Text = " ویرایش اطلاعات اولیه قرارداد با خطا مواجه شد.";
    
      }
  }
  protected void DropDownVillage1_TextChanged(object sender, EventArgs e)
  {

  }
  protected void LinkButtonedit_Click(object sender, EventArgs e)
  {
      if (ListVillages.Items.Count == 0)
      {
          Lblstatus.Text = ".لیست خالی میباشد";
          Lblstatus.CssClass = "error";
      }
      if (ListVillages.Items.Count > 0)
      {
          if (ListVillages.SelectedIndex >= 0)
          {
              if (DropDownvillage.SelectedItem.Value!="")
              {
                  SubSonic.Query qad = new SubSonic.Query(DataAccess.TblContract.Schema);
                  qad.QueryType = SubSonic.QueryType.Update;
                  qad.AddUpdateSetting(DataAccess.TblContract.Columns.VillageId, DropDownvillage.SelectedValue);
                  qad.WHERE(DataAccess.TblContract.Columns.ContractNumber, Session["ContractNumber"]);
                  qad.WHERE(DataAccess.TblContract.Columns.UserId, Session["UserId"]);
                  qad.WHERE(DataAccess.TblContract.Columns.VillageId, ListVillages.SelectedValue);
                  qad.BuildUpdateCommand();
                  qad.Execute();

                  List<Guid> dt = Session["Villages"] as List<Guid>;
                  dt.RemoveAt(ListVillages.SelectedIndex);
                  dt.Add(new Guid(DropDownvillage.SelectedValue));
                  Session["Villages"] = dt;
                  filllist();
                  Lblstatus.Text = ".روستای مورد نظر ویرایش گردید ";
                  Lblstatus.CssClass = "success";
              }
              else
              {
                  Lblstatus.Text = ".روستای جایگزین را انتخاب کنید ";
                  Lblstatus.CssClass = "error";
              }
          }
          else
          {
              Lblstatus.Text = ".لطفا ابتدا روستایی را به منظور ویرایش انتخاب بفرمایید ";
              Lblstatus.CssClass = "error";
          }
      }

  }
  protected void Button7_Click(object sender, EventArgs e)
  {
      MultiViewAdmin.ActiveViewIndex = 2;
  }
  protected void Button8_Click(object sender, EventArgs e)
  {
      clearadmin();
      MultiViewAdmin.ActiveViewIndex = -1;
      RadioButtonList1.ClearSelection();
  }


  protected void PopupControlExtender1_Dispose(object sender, EventArgs e)
  {
      if (Session["NameFamily"] != null)
      {
        
          PopupControlExtender1.Commit(Session["NameFamily"].ToString());
         
      }
  }
}